package com.kingtone.rongji;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kingtone.rongji.biz.CommonMessage;
import com.kingtone.rongji.network.UserRequest;

public class PasswordStep2Activity extends BaseActivity {

	private static final String TAG = "PasswordStep2Activity";

	private TextView emailView;
	
	private TextView phoneView;
	
	private String userName;
	
	private int userId;
	
	private String email;
	
	private String cellPhone;
	
	private int findWay = -1;
	
	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		this.setContentView(R.layout.activity_find_pwd_step2);
		setupView();
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		userName = getIntent().getExtras().getString("UserName");
		userId = getIntent().getExtras().getInt("UserId");
		
		email = getIntent().getExtras().getString("Email");
		String rEmail= getResources().getString(R.string.lable_find_by_email_content);     
		String fEmail = String.format(rEmail, email);   
		emailView.setText(fEmail);
		
		String cellPhone = getIntent().getExtras().getString("CellPhone");
		String phoneStr= getResources().getString(R.string.lable_find_by_phone_content);     
		String fPhone = String.format(phoneStr, cellPhone);   
		phoneView.setText(fPhone);
		
		findWay = -1;
	}

	private void setupView() {
		
		initTitleView(getString(R.string.title_find_pwd));
		
		LinearLayout emailV = (LinearLayout) this.findViewById(R.id.id_find_by_email);
		LinearLayout phoneV = (LinearLayout) this.findViewById(R.id.id_find_by_phone);

		phoneView = (TextView) this.findViewById(R.id.phone_content);
		emailView = (TextView) this.findViewById(R.id.email_content);
		
		emailV.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				findWay = 0;
				UserRequest userRequest = new UserRequest();
				userRequest.resetByEmail(userName, email, PasswordStep2Activity.this);
			}
		});
		
		phoneV.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				findWay = 1;
				UserRequest userRequest = new UserRequest();
				userRequest.resetByEmail(userName, cellPhone, PasswordStep2Activity.this);
			}
		});
	}
	
	@Override
	public void onBussinessSuccess(Object obj) {
		// TODO Auto-generated method stub
		super.onBussinessSuccess(obj);
		if(obj instanceof CommonMessage)
		{
			if(findWay == 0)
			{
				Intent intent = new Intent(PasswordStep2Activity.this, PasswordStep21Activity.class);
				intent.putExtra("Email", email);
				startActivity(intent);
			}
			else if(findWay == 1)
			{
				Intent intent = new Intent(PasswordStep2Activity.this, PasswordStep22Activity.class);
				intent.putExtra("CellPhone", cellPhone);
				intent.putExtra("UserId", userId);
				intent.putExtra("Token", ((CommonMessage) obj).getTag());
				startActivity(intent);
			}
		}
	}
}
